package com.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@EnableCaching // 开启缓存
@EnableTransactionManagement(proxyTargetClass = true) // 开启事务
@ComponentScan(basePackages = { "com" }, excludeFilters = {
		@Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
@PropertySource("classpath:conf.properties")
public class RootConfig {
	private static final Log LOG = LogFactory.getLog(RootConfig.class);
	@Autowired
	Environment env;

	/**
	 * configure database connection pool com.alibaba.druid.pool.DruidDataSource
	 */
	@Bean
	public DataSource dataSource() {
		DruidDataSource ds = new DruidDataSource();
		ds.setDriverClassName(env.getProperty("jdbc.driver"));
		ds.setUsername(env.getProperty("jdbc.user"));
		ds.setPassword(env.getProperty("jdbc.password"));
		ds.setUrl(env.getProperty("jdbc.url"));
		// ds.setInitialSize(Integer.parseInt(env.getProperty("jdbc.initialSize")));
		// ds.setMaxActive(Integer.parseInt(env.getProperty("jdbc.maxActive")));
		// ds.setMinIdle(Integer.parseInt(env.getProperty("jdbc.minIdle")));
		// ds.setMaxWait(Integer.parseInt(env.getProperty("jdbc.maxWait")));
		// 设置sql监控，防火墙，日志记录
		// ds.setFilters("stat,wall,log4j");
		// 设置每个连接上PSCache的大小
		// ds.setPoolPreparedStatements(true);
		// ds.setMaxPoolPreparedStatementPerConnectionSize(20);
		LOG.info(env.getProperty("jdbc.driver"));
		try {
			// ds.setFilters("log4j");
			ds.init();
		} catch (SQLException e) {
			LOG.error("ds init error: " + e.getMessage());
		}
		return ds;
	}

	@Bean
	public NamedParameterJdbcTemplate jdbcTemplate() {
		return new NamedParameterJdbcTemplate(dataSource());
	}

	/**
	 * 事务还需搭配注解开启事务和事务代理
	 * 
	 * @return
	 */
	@Bean
	public DataSourceTransactionManager transactionManager() {
		return new DataSourceTransactionManager(dataSource());
	}

	@Bean
	public CacheManager cacheManager() {
		// Spring 内置缓存管理器，将缓存存在内存中，使用ConcurrentHashMap管理数据
		
		return new ConcurrentMapCacheManager();
	}
}
